package com.quanxi.qxaimain;


import com.alibaba.fastjson.JSON;
import com.quanxi.qxaimain.common.globalException.GlobalException;
import com.quanxi.qxaimain.common.globalException.ResultCode;
import net.sf.json.JSONArray;

import java.util.*;
import java.util.concurrent.*;

/**
 * @Author lyx
 * @create 2022/9/5 12:23
 */
public class test
{

    public static void main(String[] args) throws Exception {
        invoke("tt");
    }

    /**
     * @param threadNum 线程池的数量
     * @param timeOut   超时时限
     * @return 返回执行结果
     */
  private   static String invoke(String phoneNum) throws Exception{
        String area = null;
        ExecutorService exec = Executors.newFixedThreadPool(1);
        Callable<String> call = ()-> {
            //模拟延时3秒
            Thread.sleep(4000);
            return "result888";
        };
        Future<String> future = null;
        try {
            future = exec.submit(call);
            //最长等待6秒
            area = future.get(3, TimeUnit.SECONDS);
            System.out.println(area+"777");
            exec.shutdown();
        }
        catch(java.util.concurrent.TimeoutException e1) {
            if (future != null) {
                //超时后，如果不取消任务，线程还会继续执行
                future.cancel(true);
            }
            throw e1;
        }
        catch(Exception e2) {
            if (future != null) {
                future.cancel(true);
            }
            throw e2;
        }
        finally {
            if (exec != null) {
                //任务执行完成后，要进行shutdown，否则会一直残留线程占用资源
                exec.shutdown();
            }
        }
        System.out.println("exit!");
        return area;
    }






}